package gui;
//
import javax.swing.*;
import javax.swing.border.TitledBorder;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import java.util.HashSet;
import javax.swing.ScrollPaneConstants;

import aimpublisher.AIMPublisher;
import configuration.Configuration;
import configuration.NoEntryException;

public class GUI{
	private static Configuration configuration = AIMPublisher.getConfiguration();
	
	static String selected_module = ""; //Used for the current selected module
	static String selected_entry = ""; //Used for the current selected identifier
	static String selected_tag = ""; //Used for the current selected tag
	
	public static void doGUI(){
		JFrame gui = new JFrame("AIM Publisher Configuration");
		
		final JList modules = new JList();
		final JList entries = new JList();
		final JList tags = new JList();
		final JTextField tag_value = new JTextField("", 10);
		tag_value.setVisible(true);
		tag_value.setEditable(true);
		
		//=====================Managing Modules=============================//
		
		//Retrieving a set of modules and adding to a list
		DefaultListModel modules_listModel = new DefaultListModel();
		HashSet<String> modules_set = new HashSet<String>(configuration.getModules());
		for (String i : modules_set){
			modules_listModel.addElement(i);
		}
		modules.setModel(modules_listModel);
		
		
		//Configuring the list
		modules.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		modules.setLayoutOrientation(JList.VERTICAL);

		//Selecting a module from  the list
		modules.addListSelectionListener(new ListSelectionListener(){
			@Override
			public void valueChanged(ListSelectionEvent event) {
				String name = (String) modules.getSelectedValue();
				if(name != null && configuration.isModule(name)){
					selected_module = name;
					try {//Try to update the list model for entries
						DefaultListModel entries_listModel = new DefaultListModel();
						HashSet<String> entries_set = new HashSet<String>(configuration.getEntries(selected_module));
						for (String i : entries_set){
							entries_listModel.addElement(i);
						}
						entries.setModel(entries_listModel);
						tags.setModel(new DefaultListModel());//have to clear the tags list because swing doesn't do it for us
					} catch(NoEntryException e1){
						;//Should not get here because we check that module exists
					}
					tag_value.setText("");
				}
			}});
		JButton new_mod_button = new JButton("Add Module");
		new_mod_button.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				
		String mod = JOptionPane.showInputDialog("Enter Name of Module");
		boolean test = false;
		while(test == false) {
		if(!configuration.isModule(mod)) {
			test = true;
			break;
		}
		mod = JOptionPane.showInputDialog("Module already Defined, Enter Name of Module");
		}
		
		try {
			configuration.addModule(mod);
		}
		catch (Exception e) { }
		HashSet<String> modules_set = new HashSet<String>(configuration.getModules());
		DefaultListModel modules_listModel = new DefaultListModel();
		for (String i : modules_set){
			modules_listModel.addElement(i);
		}
		modules.setModel(modules_listModel);
		
			}});

		//Making a box for the modules, and adding the box and the list to frame

		JPanel content = new JPanel();
		content.setLayout(new BoxLayout(content,BoxLayout.X_AXIS));
		gui.setContentPane(content);
		
		JPanel modulesPanel = new JPanel();
		modulesPanel.setLayout(new GridLayout(2,0));
		modulesPanel.setBorder(new TitledBorder("Modules"));
		JScrollPane modPane = new JScrollPane(modules);
		modPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		modulesPanel.add(modPane);
		modulesPanel.add(new_mod_button);
		content.add(modulesPanel);
		//Box modules_box = new Box(BoxLayout.Y_AXIS);
		//modules_box.add(modules);
		//modules_box.add(select_module_button);
		//gui.add(modules_box);

		
		
		//=====================Managing Identifiers==========================//
		
		//Configuring the list of identifiers
		entries.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		entries.setLayoutOrientation(JList.VERTICAL);
		

		//Selecting an identifier from  the list
		
		entries.addListSelectionListener(new ListSelectionListener(){
			@Override
			public void valueChanged(ListSelectionEvent event) {
				String name = (String) entries.getSelectedValue();
				if(configuration.isModule(selected_module)){
					if(name != null && configuration.isEntry(selected_module, name)){
						selected_entry = name;
						try {//Try to update the list model for entries
							DefaultListModel tags_listModel = new DefaultListModel();
							HashSet<String> tags_set = new HashSet<String>(configuration.getTags(selected_module, selected_entry));
							for (String i : tags_set){
								tags_listModel.addElement(i);
							}
							tags.setModel(tags_listModel);
						} catch(NoEntryException e1){
							;//Should not get here because we check that entry and module exist
						}
						
					}
				}
			}});
		
		
		//Making a box for the identifiers, and adding the box and the list to frame

		JButton new_id_button = new JButton("Add ID");
		new_id_button.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(!selected_module.equals("")) {
		String id = JOptionPane.showInputDialog("Enter Name of Identifier");
		boolean test = false;
		while(test == false) {
		if(!configuration.isEntry(selected_module, id)) {
			test = true;
			break;
		}
		id = JOptionPane.showInputDialog("Identifier already Defined, Enter Name of Identifier");
		}
		
		try {
			configuration.addID(selected_module, id);
		}
		catch (Exception e) { }
		
			}
			}
		});

		
		JPanel entriesPanel = new JPanel();
		entriesPanel.setLayout(new GridLayout(2,0));
		entriesPanel.setBorder(new TitledBorder("Entries"));
		JScrollPane entryPane = new JScrollPane(entries);
		entryPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		entriesPanel.add(entryPane);
		entriesPanel.add(new_id_button);
		content.add(entriesPanel);

		
		/*Adding an identifier
		JButton add_entry_button = new JButton("Add Identifier");
		add_entry_button.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				//making sure we have a valid selected module
				if(configuration.isModule(selected_module)){
					String new_entry = JOptionPane.showInputDialog(null, "Please enter the name for the identifier");//Need to invoke a dialog and whatnot here
					//configuration.Add
				}
				
			}});
		entries_box.add(add_entry_button);
		*/
		
		//=====================Managing Tags=================================//
		//Configuring the list of tags
		tags.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		tags.setLayoutOrientation(JList.VERTICAL);
	
		//Viewing tag values
		
		tags.addListSelectionListener(new ListSelectionListener(){
			@Override
			public void valueChanged(ListSelectionEvent event) {
				if(configuration.isModule(selected_module)){
					if(configuration.isEntry(selected_module, selected_entry)){
						String name = (String) tags.getSelectedValue();
						if(configuration.isEntry(selected_module, selected_entry, name)){
							selected_tag = name;
							try{
								String value = configuration.getEntry(selected_module, selected_entry,selected_tag);
								tag_value.setText(value);
							} catch (NoEntryException e1){
								;//Should not get here because we check ahead of time
							}
					}
					}
				}
				
			}});

		//Making a box for the tags, and adding the box and the list to frame
		
		JPanel tagsPanel = new JPanel();
		tagsPanel.setLayout(new GridLayout(2,0));
		//tagsPanel.setLayout(new BoxLayout(tagsPanel,BoxLayout.Y_AXIS));
		tagsPanel.setBorder(new TitledBorder("Tags"));
		JScrollPane tagPane = new JScrollPane(tags);
		tagPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		tagsPanel.add(tagPane);		
		

		
		//=====================Managing Values===============================//
		//Changing tag values
		JButton change_tag_value_button = new JButton("Change Value");
		change_tag_value_button.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(configuration.isEntry(selected_module, selected_entry, selected_tag)){
					String new_value = tag_value.getText();
					try{
						configuration.changeEntry(selected_module, selected_entry, selected_tag, new_value);
					} catch (NoEntryException e1){
						;//Shouldn't happen
					}
					
				}
				
			}});
		JButton new_item_button = new JButton("Add Item");
		new_item_button.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(!selected_module.equals("") && !selected_entry.equals("")) {
		String tag = JOptionPane.showInputDialog("Enter Name of Tag");
		boolean test = false;
		while(test == false) {
		if(!configuration.isEntry(selected_module, selected_entry, tag)) {
			test = true;
			break;
		}
		tag = JOptionPane.showInputDialog("Tag already Defined, Enter Name of Tag");
		}
		String val = JOptionPane.showInputDialog("Enter Value for Tag");
		try {
			configuration.addEntry(selected_module, selected_entry, tag, val);
			DefaultListModel tags_listModel = new DefaultListModel();
			HashSet<String> tags_set = new HashSet<String>(configuration.getTags(selected_module, selected_entry));
			for (String i : tags_set){
				tags_listModel.addElement(i);
			}
			tags.setModel(tags_listModel);
		}
		catch (Exception e) { }
		
			}
				
			}
		});
		
		
		tagsPanel.add(new_item_button);
		content.add(tagsPanel);
		
		JPanel viewPanel = new JPanel();
		viewPanel.setBorder(new TitledBorder("Values"));
		
		tag_value.setSize(50,50);
		viewPanel.add(tag_value);
		viewPanel.add(change_tag_value_button);
		viewPanel.setLayout(new GridLayout());
		content.add(viewPanel);
		gui.setSize(600, 250);

		gui.setVisible(true);
	}

	
}
